<template>
	<view class="yg-cat">
		<search></search>
		<!-- 搜索中心结束 -->
		<view class="yg-cat-content">
			<scroll-view class="cat-content-left" scroll-y="true">
				<view :class="['content-left-item',{'active':select_index===index?true:false}]"
					@click="handleTabClick(index)" v-for="(item,index) in categories" :key="index">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 左边菜单的结束 -->
			<!-- 右边菜单 -->
			<scroll-view class="cat-content-right" scroll-y="true" @scrolltolower="handleScrollToLower"  :scroll-top="scrollTop">
				<view class="content-right-list">
					<view class="right-list-item" v-for="(item,index) in rightContent" :key="item.cat_id">
						<view class="list-item-title">
							/{{item.cat_name}}/
						</view>
						<view class="list-item-l">
							<navigator :url="'/pages/goods_list/goods_list?cid='+v.cat_id" class="item-l-i"
								v-for="(v,i) in item.children" :key="v.cat_id">
								<image :src="v.cat_icon" mode=""></image>
								<view class="title">
									{{v.cat_name}}
								</view>
							</navigator>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import Search from "../../components/Search.vue"
	import {
		request
	} from "@/request/request.js"
	export default {
		components: {
			"search": Search
		},
		async onShow() {
			let res = await request({
				url: "categories"
			});
			this.categories = res.message;
			this.rightContent = res.message[0].children;

		},

		data() {
			return {
				categories: [],
				select_index: 0,
				rightContent: [],
				scrollTop: 0
			}
		},
		methods: {

			handleTabClick: function(index) {
				this.select_index = index;
				this.rightContent=this.categories[index].children
				this.scrollTop=1
				this.$nextTick(()=>{
					console.log('到顶部去')
					this.scrollTop=0
				})
			},
			handleScrollToLower:function(){
				let next_index=(this.select_index+1)%(this.categories.length)
				this.select_index=next_index
				this.rightContent=this.categories[next_index].children
			}
		}
	}
</script>

<style lang="less">
	.yg-cat {
		.yg-cat-content {
			display: flex;
			height: calc(100vh - 100rpx);

			.cat-content-left {
				flex: 1;

				background-color: #f3f3f3;

				.active {
					background: #FFFFFF;
					color: #4cd964;
				}
			}

			.content-left-item {
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
			}

			.cat-content-right {
				flex: 3;


				.list-item-title {
					height: 80rpx;
					text-align: center;
					line-height: 80rpx;

				}

				.list-item-l {
					display: flex;
					flex-wrap: wrap;

					.item-l-i {
						width: 33.33%;
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;

						image {
							height: 120rpx;
							width: 120rpx;
						}
						.title{
							font-size: 20rpx;
						}
					}
				}
			}
		}
	}
</style>
